<?php

namespace app\admin\controller;

use app\Base;
use app\service\admin\IndexService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

/**
 * 后台首页
 *
 * Class Index
 * @package projects\controller\admin
 * @author Du
 * @date 2021/1/14
 * @time 10:23
 */
class Index extends Base
{

    /**
     * 菜单导航
     * User : Du
     * Date : 2022/11/22
     * Time : 12:10
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function menu(IndexService $service): View
    {
        $res = $service::getData();
        return view('index', ['data' => $res]);
    }

    /**
     * 默认首页
     * User : Du
     * Date : 2022/11/22
     * Time : 12:10
     * @return View
     */
    public function home(): View
    {
        return view('home/index');
    }

    /**
     * 绑定用户
     * @param IndexService $service
     * @return Json
     * @throws \Exception
     */
    public function noticeBindUser(IndexService $service): Json
    {
        $res = $service::noticeBindUser($this->params);
        return $this->returnData();
    }


    /**
     * 微信绑定信息
     *
     * User : Du
     * Date : 2022/11/22
     * Time : 18:32
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function wxbinb(IndexService $service): View
    {
        $res = $service::getWxBind();
        return view('admin/home/wx', ['data' => $res]);
    }

    /**
     * 用户绑定
     * User : Du
     * Date : 2022/11/22
     * Time : 22:52
     * @param IndexService $service
     * @return Json
     */
    public function userBind(IndexService $service): Json
    {
        $res = $service::userBind();
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 用户绑定扫码回调
     * User : Du
     * Date : 2022/11/23
     * Time : 14:26
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function WxCallback(IndexService $service): View
    {
        $res = $service::userScanCode($this->params);
        if (is_string($res)) {
            $data = [
                'status' => 1,
                'msg' => $res,
            ];
        } else {
            $data = [
                'status' => 0,
            ];
        }
        return view('admin/mobile/wxLogin', ['data' => $data]);
    }

    /**
     * 获取绑定状态
     * User : Du
     * Date : 2022/11/23
     * Time : 16:17
     * @param IndexService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function bindingStatus(IndexService $service): Json
    {
        $res = $service::bindingStatus($this->params);
        if (!$res) {
            return $this->returnData('fail', '请求错误，无记录');
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 解除绑定
     * User : Du
     * Date : 2022/11/23
     * Time : 17:18
     * @param IndexService $service
     * @return Json
     * @throws DbException
     */
    public function userWxUnbind(IndexService $service): Json
    {
        $res = $service::userWxUnbind();
        if (!$res) {
            return $this->returnData('fail', '解绑失败！');
        }
        return $this->returnData();
    }
}
